//获取应用实例
const SERVER = require('../../utils/public');
const app = getApp()
// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    des: "",
    list:"",//品饮笔记的变量
    wieid:"",//酒的id
    count_want:'',
    count_drinked:'',
    type:0,//控制type切换
    tap:false,
    taps:false,
    type:1,
    dis:true,
    listote:"",
    shao:""
  },
//点击跳转酒厂
  jiuc:function(e){
    let data = e.currentTarget.dataset.name
    console.log(data)
    wx.navigateTo({
      url: '/pages/details/index?id=' + 2 + "&brandid=" + data,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
       
      }
    })
  },
  ziliao(e){

    this.setData({
      type: e.currentTarget.dataset.index
    })
  },
  //点击进入笔记详情
  drinde:function(e){
    wx.navigateTo({
      url: '../dired/dired?wenid=' +e.currentTarget.dataset.item.wineid + '&uid=' +e.currentTarget.dataset.item.uid,
    })
  },
  //保存图片
  eventSave: function () {
    let that = this;
    console.log(that.data.des.pic)
    wx.getSetting({
      success(res) {
        //未授权 先授权 然后保存
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(re) {
              that.saveToBlum();
            }
          })
        } else {
          //已授 直接调用保存到相册方法
          that.saveToBlum();
        }
      }
    })
  },
  //保存图片方法
  saveToBlum: function () {
    let that = this;
    console.log(that.data.des.pic)
    wx.downloadFile({
      url: that.data.des.pic,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(result) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  clickimg:function(){
    this.setData({
      dis:!this.data.dis
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
      console.log(options)
      let that = this
      that.setData({
        wieid: options.id,
        type: options.type
      })
   
  
      // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    if(options.fen==123){
      wx.setStorageSync('wiedi', this.data.wieid)
      app.request({
        url: '/whisky/get-whisky?wineid=' + options.id,

        success: function (res) {

          that.setData({
            des: res.data,
            shao: res.data.otw,
            count_want: res.data.count_want,
            count_drinked: res.data.count_drinked,
            imgsurl: res.data.pic,
            // tap: res.data.user_behavior.want,
            // taps: res.data.user_behavior.drinked,

          })
          // console.log(res.data.otw)
        }
      })

      app.request({//调用品饮笔记的接口
        url: '/selectNote',
        data:{
          "wineid":options.id 
        },
        method:'post',
        
        success: function (res) {
          res.data.list.map((item, i) => {
            res.data[i].labelEntry = item.labelEntry.split(',')
            res.data[i].labelMiddle = item.labelMiddle.split(',')
            res.data[i].labelSmell = item.labelSmell.split(',')
            res.data[i].labelTaste = item.labelTaste.split(',')
            res.data.list[i].color = item.color.split(',')
          })

          that.setData({
            list: res.data
          })
          console.log(res.data.list, '笔记')
          // that.setData({ des: res.data })
        }
      })
    }else{
      wx.setStorageSync('wiedi', this.data.wieid)
      app.request({
        url: '/selectWhiskyBy',
        data:{
          'wineid':options.id ,
        },
        method:'post',
        success: function (res) {
          console.log(res)
          that.setData({
            des: res.data.whisky,
            shao: res.data.otw,
            count_want: res.data.whisky.count_want,
            count_drinked: res.data.whisky.count_drinked,
            imgsurl: res.data.whisky.pic,
            tap: res.data.want,
            taps: res.data.drinked,
          })
          // console.log(res.data.otw)
        }
      })

      app.request({//调用品饮笔记的接口
        url: '/selectNote',
        data:{
          "wineid":options.id 
        },
        method:'post',

        success: function (res) {
          res.data.map((item, i) => {
            res.data[i].labelEntry = item.labelEntry.split(',')
            res.data[i].labelMiddle = item.labelMiddle.split(',')
            res.data[i].labelSmell = item.labelSmell.split(',')
            res.data[i].labelTaste = item.labelTaste.split(',')
            res.data[i].color = item.color.split(',')
          })

          that.setData({
            list: res.data
          })
          console.log(res, '笔记')
          // that.setData({ des: res.data })
        }
      })
    }
   
    // that.getDes(options.id)
  },
  //点击想和按钮的接口
  miss:function(){
    if (wx.getStorageSync("tk") == '123'){
      this.setData({
        tap: true
      })
      let that = this;
      app.request({
        url: '/WhiskyWantInsert',
        data:{
          'wineid': this.data.wieid
        },
        method:'post',
        success: function (res) {
          console.log(res)
          if (res.code == 200) {
            wx.showToast({
              title: '添加成功',
              icon: 'succes',
              duration: 500,
              mask: true
            })
            that.setData({
              count_want: that.data.count_want + 1
            })
          }
          else {
            wx.showToast({
              title: '用户已标注了喝过~',
              icon: 'succes',
              duration: 500,
              mask: true
            })
          }


        }
      })
    }else{
      wx.navigateTo({
        url: '../index/index',
      })
    }
    
  },
  Test:function(){
    if (wx.getStorageSync("tk") == '123'){
      this.setData({
        taps: true
      })
      let that = this;
      app.request({
        url: '/WhiskyDrinkedInsert',
        data:{
          'wineid': this.data.wieid
        },
        method:'post',
        success: function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: '添加成功',
              icon: 'succes',
              duration: 500,
              mask: true
            })

            that.setData({
              count_drinked: that.data.count_drinked + 1
            })
          } else {
            wx.showToast({
              title: '已经添加过了哦！',
              icon: 'succes',
              duration: 500,
              mask: true
            })
          }

        }
      })
    }else{
      wx.navigateTo({
        url: '../index/index',
      })
    }
    
  },

//点击品饮笔记
pinyin:function(e){
  if (wx.getStorageSync("tk")=='123'){
    var wieid = wx.getStorageSync("wiedi")
  wx.setStorageSync('fenshu', this.data.des.wb_score)
  wx.navigateTo({
    url: '../Pinyin/Pinyin?id='+wieid+'&img='+this.data.imgsurl,
  })
  }else{
    wx.navigateTo({
      url: '../index/index',
    })
  }
  
},
  getDes(data){
    console.log(JSON.stringify(data)+'idididi')
    
    let that = this
    let datas = {
      // uid:wx.getStorageSync('uid'),
      wineid: data.wineid
    }
    let uid={
      uid: wx.getStorageSync("uid"),
      wineid: data.wineid
      // uid:7,
      // wineid:123
    }
    app.request({
      url: '/whisky/get-whisky',
      data: datas,
      success: function(res) {
        that.setData({
          des: res.data,
          // type: res.data.user_behavior.want,
          // types: res.data.user_behavior.drinked
          })
        console.log(res.data)
      }
    })
    // app.request({//调用品饮笔记的接口
    //   url: '/selectNoteList',
    //   data: uid,
    //   success: function (res) {

    //       res.data.list.map((item, i) => {
    //         res.data.list[i].label_entry = item.label_entry.split(',')
    //         res.data.list[i].label_middle = item.label_middle.split(',')
    //         res.data.list[i].label_smell = item.label_smell.split(',')
    //         res.data.list[i].label_taste = item.label_taste.split(',')
    //         that.setData({
    //           list: res.data.list
    //         })
    //       })
    //       that.setData({
    //         list: res.data.list
    //       })
        
       
    //     // that.setData({ des: res.data })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    path: '/pages/list/list?id='+this.data.wineid+"&fen=123"
  }
})